package org.seedltd.upms.service.impl.detection;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.AllArgsConstructor;
import org.seedltd.core.base.BaseServiceImpl;
import org.seedltd.core.utils.BeanUtils;
import org.seedltd.springsecurity.utils.UserUtils;
import org.seedltd.upms.mapper.detection.DetectionMpConfigMapper;
import org.seedltd.upms.model.detection.entity.DetectionMpConfig;
import org.seedltd.upms.model.detection.form.DetectionMpConfigForm;
import org.seedltd.upms.model.detection.vo.DetectionMpConfigVO;
import org.seedltd.upms.service.detection.DetectionMpConfigService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 小程序配置业务实现
 * 
 * @author laizuan
 * @since 2021/08/12
 */
@Service
@CacheConfig(cacheNames = "DetectionMpConfig")
@AllArgsConstructor
public class DetectionMpConfigServiceImpl extends BaseServiceImpl<DetectionMpConfigMapper, DetectionMpConfig>
    implements DetectionMpConfigService {

    @Override
    @Cacheable(key = "#id", unless = "#result == null")
    public DetectionMpConfigVO findById(Long id) {
        DetectionMpConfig pojo = baseMapper.selectById(id);
        if (pojo == null) {
            DetectionMpConfig detectionMpConfig = new DetectionMpConfig();
            detectionMpConfig.setId(UserUtils.getCompanyId());
            this.save(detectionMpConfig);
            return null;
        }
        DetectionMpConfigVO resultVo = new DetectionMpConfigVO();
        BeanUtils.copyProperties(pojo, resultVo);
        return resultVo;
    }

    @Override
    @CacheEvict(key = "#form.id")
    public boolean update(DetectionMpConfigForm form) {
        UpdateWrapper<DetectionMpConfig> updateWrapper =
            new UpdateWrapper<DetectionMpConfig>().set(StrUtil.toUnderlineCase(form.getField()), form.getValue()).eq("id", form.getId());
        return this.update(updateWrapper);
    }

    /*private String sendEmail(TContact tContact) {

        MailAccount account = new MailAccount();
        account.setHost(tNotifyConfig.getMailHost());
        account.setPort(Integer.valueOf(tNotifyConfig.getMailPort()));
        account.setFrom(tNotifyConfig.getSendFrom());
        account.setUser(tNotifyConfig.getUsername());
        account.setPass(tNotifyConfig.getPass());
        account.setSslEnable(tNotifyConfig.getSslEnable() == null || tNotifyConfig.getSslEnable() == 1);

        String contactTos = tNotifyConfig.getContactTos();
        String[] tos = StringUtils.split(contactTos, ",");
        ArrayList<String> tosList = Lists.newArrayList(tos);
        String contactCcs = tNotifyConfig.getContactCcs();
        String[] ccs = StringUtils.split(contactCcs, ",");
        List<String> ccList = null;
        if (ccs != null) {
            ccList = Lists.newArrayList(ccs);
        }

        StringBuilder sb = new StringBuilder();
        sb.append("<div style=\"width:500px;margin: 0 auto;\">");
        sb.append("<p>留言人姓名：")
                .append(StringUtils.isEmpty(tContact.getUsername()) ? "" : tContact.getUsername())
                .append("</p>");
        sb.append("<p>单位名称：")
                .append(StringUtils.isEmpty(tContact.getUnitName()) ? "" : tContact.getUnitName())
                .append("</p>");
        sb.append("<p>联系电话：")
                .append(StringUtils.isEmpty(tContact.getContactNo()) ? "" : tContact.getContactNo())
                .append("</p>");
        sb.append("<p>留言内容：")
                .append(StringUtils.isEmpty(tContact.getMessage()) ? "" : tContact.getMessage())
                .append("</p>");
        sb.append("<p>留言时间：")
                .append(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, tContact.getCreateTime()))
                .append("</p>");
        sb.append("</div>");
        return MailUtil.send(account, tosList, ccList, null, "新留言 - 【联系我们】", sb.toString(), true);
    }*/

}